package com.usian.filter;

import cn.hutool.jwt.JWTUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.LinkedTransferQueue;

/*
 * 作者：m鹏h
 * 时间；2023/8/22 15:21
 * 描述：
 **/
@Component
public class MyFilter implements GlobalFilter , Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String token = request.getHeaders().getFirst("token");
        String path = request.getURI().getPath();

        if (StringUtils.isNoneBlank(token))if (JWTUtil.verify(token,"111".getBytes()))return chain.filter(exchange);

        if (path.contains("login")||path.contains("export"))return chain.filter(exchange);

        response.setStatusCode(HttpStatus.UNAUTHORIZED);return response.setComplete();

    }

    @Override
    public int getOrder() {
        return 1;
    }
}
